package com.waq.controller;

import com.waq.mapper.RegisterMapper;
import com.waq.pojo.Register;
import com.waq.pojo.bo.RegisterBO;
import com.waq.service.RegisterService;
import com.waq.utils.WAQJSONResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RequestMapping("register")
@RestController
@Controller
public class RegisterController {
    @Autowired
    private RegisterService registerService;

    @PostMapping(value = "/queryRegisterList")
    public WAQJSONResult queryRegisterList(@RequestBody Register register) {
        if (StringUtils.equals(register.getRegisterId(), "-1")) {
            return WAQJSONResult.ok();
        }
        List<Register> result = registerService.queryRegisterList(register);
        return WAQJSONResult.ok(result);
    }

    @RequestMapping("/registerCustomer")
    public WAQJSONResult registerCustomer(@RequestBody Register register) {
        Register register = registerService.registerCustomer(register);
        if (register == null) {
            return WAQJSONResult.errorMsg("登记失败，数据库返回结果为空");
        }
        return WAQJSONResult.ok();
    }

    @RequestMapping("/changeRegister")
    public WAQJSONResult changeRegister(@RequestBody Register register) {
        if (StringUtils.isBlank(register.getRegisterId())) {
            return WAQJSONResult.errorMsg("没有获得到登记号");
        }
        if (register.getCustomerId() == null || register.getCustomerId() == 0) {
            return WAQJSONResult.errorMsg("没有获得到会员号");
        }
        register = registerService.changeRegister(register);
        if (register == null) {
            return WAQJSONResult.errorMsg("更新数据库内信息失败");
        }
        return WAQJSONResult.ok();
    }
}
